package api;

import conf.APP;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.HttpClientUtils;
import utils.ResponseData;

import java.util.HashMap;
import java.util.Map;

public class UserApi {
    private static final String addUrl = APP.BASE_URL + "/member/member/addUserSubmit";
    private static final Logger logger = LoggerFactory.getLogger(CommonApi.class);
    //修改用户密码
    private static final String updateUserPwdUrl = APP.BASE_URL + "/member/member/editUsersubmit";


    /**
     * @param name   用户名
     * @param mobile 手机号
     * @param pwd    密码
     * @param rePwd  确认密码
     * @param role   用户角色[1:投资人；2:借款人]
     * @return 构造后的响应数据
     */
    public ResponseData addUser(String name, String mobile, String pwd, String rePwd, String role) throws Exception {
        logger.info("name={},mobile={},pwd={},rePwd={},role={}", name, mobile, pwd, rePwd, role);
        Map<String, String> formParams = new HashMap<>();
        if (name != null) {
            formParams.put("name", name);
        }
        if (mobile != null) {
            formParams.put("phone", mobile);
        }
        if (pwd != null) {
            formParams.put("pwd", pwd);
        }
        if (rePwd != null) {
            formParams.put("rePwd", rePwd);
        }
        if (role != null) {
            formParams.put("role", role);
        }
        logger.info(String.valueOf(formParams));
        return HttpClientUtils.post(addUrl, formParams);
    }
    //修改用户密码
    public ResponseData updateUserPwd(String id, String pwd, String rePwd, String pwdAttach) throws Exception {
        logger.info("id={},pwd={},rePwd={},pwdAttach={}", id, pwd, rePwd, pwdAttach);
        Map<String, String> formParams = new HashMap<>();
        if (id != null) {
            formParams.put("id", id);
        }
        if (pwd != null) {
            formParams.put("pwd", pwd);
        }
        if (rePwd != null) {
            formParams.put("rePwd", rePwd);
        }
        if (pwdAttach != null) {
            formParams.put("pwd_attach", pwdAttach);
        }
        logger.info(String.valueOf(formParams));
        return HttpClientUtils.post(updateUserPwdUrl, formParams);

    }
    //修改用户锁定状态

    /**
     *
     * @param id 用户id
     * @param status 状态[1:正常；-1:系统锁定]
     * @return 构造后的响应数据
     */
    public ResponseData modifyUserStatus(String id,String status) throws Exception {
        logger.info("id ={} status={}",id,status);
        String url = APP.BASE_URL+"member/member/editLockSubmit";
        Map<String,String> form = new HashMap<>();
        form.put("id",id);
        form.put("status",status);
        return HttpClientUtils.post(url,form);

    }
}
